/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Maestros.Vista;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import pe.edu.pucp.Maestros.Controlador.AeropuertoControlador;
import pe.edu.pucp.Modelo.Aeropuerto;
import pe.edu.pucp.Modelo.Ciudad;
import pe.edu.pucp.Modelo.Continente;
import pe.edu.pucp.Modelo.Pais;
import pe.edu.pucp.Modelo.Permiso;
import pe.edu.pucp.Util.Constante;

/**
 *
 * @author katy
 */
public class BuscarAeropuertoGUI extends javax.swing.JPanel {

    private JFrame padre;
    private AeropuertoControlador c;
    /**
     * Creates new form BuscarAeropuertoGUI
     */
    public BuscarAeropuertoGUI() {
        initComponents();
    }
   
    public JFrame getPadre()
    {
        return padre;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnCrear = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cmbContinente = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cmbPais = new javax.swing.JComboBox();
        cmbCiudad = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        dtcFechaRegistro = new com.toedter.calendar.JDateChooser();
        pnAerouertos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setMaximumSize(new java.awt.Dimension(764, 700));
        setMinimumSize(new java.awt.Dimension(764, 700));
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(764, 700));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Aeropuerto"));
        jPanel1.setPreferredSize(new java.awt.Dimension(634, 240));

        btnCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/plus.png"))); // NOI18N
        btnCrear.setMaximumSize(new java.awt.Dimension(110, 29));
        btnCrear.setMinimumSize(new java.awt.Dimension(110, 29));
        btnCrear.setPreferredSize(new java.awt.Dimension(110, 29));

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/edit.png"))); // NOI18N

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/search.png"))); // NOI18N

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/refresh.png"))); // NOI18N

        jLabel1.setText("Continente:");

        jLabel2.setText("Fecha registro: ");

        jLabel3.setText("País:");

        jLabel5.setText("Ciudad:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(42, 42, 42)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dtcFechaRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbPais, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbContinente, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 278, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(dtcFechaRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbContinente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnBuscar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pnAerouertos.setBorder(javax.swing.BorderFactory.createTitledBorder("Aeropuertos"));

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "AEROPUERTO", "CIUDAD", "CAPACIDAD", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultados.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblResultados);

        javax.swing.GroupLayout pnAerouertosLayout = new javax.swing.GroupLayout(pnAerouertos);
        pnAerouertos.setLayout(pnAerouertosLayout);
        pnAerouertosLayout.setHorizontalGroup(
            pnAerouertosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnAerouertosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        pnAerouertosLayout.setVerticalGroup(
            pnAerouertosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnAerouertosLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnAerouertos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(pnAerouertos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Agregar escucha para boton nuevo
    public void addNuevoAeropuertoListener(ActionListener listener)
    {
        this.btnCrear.addActionListener(listener);
    }
   
     // Agregar escucha para boton de edicion
    public void addEditarButtonListener(ActionListener listener) {
        this.btnEditar.addActionListener(listener);
    }

    // Agregar escucha para boton limpiar
    public void addLimpiarButtonListener(ActionListener listener) {
        this.btnLimpiar.addActionListener(listener);
    }
    
    // Agregar escucha para busqueda de aeropuertos
    public void addBuscarButtonListener(ActionListener ml)
    {
        this.btnBuscar.addActionListener(ml);
    }
    
    public void addCambiarContinente(ItemListener ml){
        this.cmbContinente.addItemListener(ml);
    }     
    
    public void addCambiarPais(ItemListener ml){
        this.cmbPais.addItemListener(ml);
    }  
    
    public int getSelectedAeropuerto() {
        return tblResultados.getSelectedRow();    
    }
       
    public Date getFechaRegistro() {
        return dtcFechaRegistro.getDate();
    }
    
     public int getCiudad(){
        if (cmbCiudad.getSelectedIndex() > 0)
             return cmbCiudad.getSelectedIndex() - 1;
        else return -1;
     } 
    
    public int getPais(){
        if(cmbPais.getSelectedIndex() > 0)
            return cmbPais.getSelectedIndex() - 1;
        else return -1;
    }     
    
    public int getContinente(){
        if (cmbContinente.getSelectedIndex() > 0)
            return cmbContinente.getSelectedIndex() - 1;
        else return -1;
    }  
    
    public void mostrarPermisos(ArrayList<Permiso> list){            
        if (!list.isEmpty()){                                
            btnBuscar.setEnabled(buscarPermiso(list,Constante.P_ADM_AER_BUSCAR)==1);
            btnCrear.setEnabled(buscarPermiso(list,Constante.P_ADM_AER_REGISTRAR)==1);
            btnEditar.setEnabled(buscarPermiso(list,Constante.P_ADM_AER_EDITAR)==1);
        }
    }    
  
    public int buscarPermiso(ArrayList<Permiso> list, int idel){
        int per=-1;
        for (Permiso opc : list){
            if (opc.getElemento().getIdelemento()==idel){ per =opc.getPermiso(); }
        }
        return per;
    }     
    
    public void llenarComboPais(ArrayList<Pais> lista){
        cmbPais.removeAllItems();
        cmbPais.addItem(Constante.DEFAULT_OPCION);
        if(lista!=null){           
            for (Pais opc : lista){
                cmbPais.addItem(opc.getDescripcion());
            }
        }
    }    
    
    public void llenarComboContinente(ArrayList<Continente> lista){
        cmbCiudad.removeAllItems();    
        cmbContinente.addItem(Constante.DEFAULT_OPCION);
        if(lista!=null){
            for (Continente opc : lista){
                cmbContinente.addItem(opc.getDescripcion());
            }
        }
    }

    public void llenarComboCiudad(ArrayList<Ciudad> lista){
        cmbCiudad.removeAllItems();        
        cmbCiudad.addItem(Constante.DEFAULT_OPCION);
        if(lista!=null){
            for (Ciudad opc : lista){
                cmbCiudad.addItem(opc.getDescripcion());
            }
        }
    }     
    
    public void llenaGrilla(ArrayList<Aeropuerto> lista){        
        String mensaje;
        if(lista!=null){ 
            ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
            for (int f = 0; f<lista.size();f++){
                ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount()+1);               
                tblResultados.setValueAt(lista.get(f).getDescripcion(),f,0);
                tblResultados.setValueAt(lista.get(f).getCiudad().getDescripcion(),f,1);
                tblResultados.setValueAt(lista.get(f).getCappack(),f,2);
                tblResultados.setValueAt(lista.get(f).getEstadoAeropuerto().getDescripcion(),f,3);
            }
            pnAerouertos.setBorder(javax.swing.BorderFactory.createTitledBorder("Aeropuertos ("+lista.size() + ")"));
        } else {
            pnAerouertos.setBorder(javax.swing.BorderFactory.createTitledBorder("Aeropuertos ("+lista.size() + ")"));
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCrear;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbCiudad;
    private javax.swing.JComboBox cmbContinente;
    private javax.swing.JComboBox cmbPais;
    private com.toedter.calendar.JDateChooser dtcFechaRegistro;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnAerouertos;
    private javax.swing.JTable tblResultados;
    // End of variables declaration//GEN-END:variables
}
